package com.xuecheng.media.controller;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.AliyunVodApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
/**
* <p></p>
*
* @Description:
*/
@Slf4j
@RestController
public class AliyunVodController implements AliyunVodApi {

    @Autowired
    private MediaService mediaService;

   @Value("cn-shanghai")
   private  String region;
   @Value("LTAI5tPM3UeS53rWcedrJX2B")
   private  String accessKeyId;
   @Value("KsAsIAoiCBcO77e0jCAt7pUat8F08f")
   private  String accessKeySecret;


   @PostMapping("media/vod-token")
   public VodUploadToken generateUploadToken(@RequestBody VodUploadRequest uploadRequest) {

       try {
           // 1.初始化客户端
           DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
           // 2.创建请求对象并获得响应对象
           CreateUploadVideoResponse response = AliyunVODUtil.createUploadVideo(client, uploadRequest.getTitle(), uploadRequest.getFileName());
           // 3.解析响应数据
           VodUploadToken token = new VodUploadToken();
           token.setVideoId(response.getVideoId());
           token.setUploadAddress(response.getUploadAddress());
           token.setUploadAuth(response.getUploadAuth());
           token.setRequestId(response.getRequestId());
           return token;
       } catch (Exception e) {
           String message = e.getMessage();
           log.error(MediaErrorCode.E_140011.getDesc()+" : {}",message);
           ExceptionCast.cast(CommonErrorCode.UNKOWN);
           return null;
       }
   }
   @GetMapping("media/refresh-vod-token/{videoId}")
   public VodUploadToken refreshUploadToken(@PathVariable String videoId) {
       try {
           // 1.初始化客户端
           DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
           // 2.创建请求对象并获得响应对象
           RefreshUploadVideoResponse response = AliyunVODUtil.refreshUploadVideo(client, videoId);
           // 3.解析响应数据
           VodUploadToken token = new VodUploadToken();
           token.setVideoId(response.getVideoId());
           token.setUploadAddress(response.getUploadAddress());
           token.setUploadAuth(response.getUploadAuth());
           token.setRequestId(response.getRequestId());
           return token;
       } catch (Exception e) {
           String message = e.getMessage();
           log.error(MediaErrorCode.E_140015.getDesc()+" : {}",message);
           ExceptionCast.cast(CommonErrorCode.UNKOWN);
           return null;
       }
   }

    @GetMapping("media/preview/{mediaId}")
    public String getPlayUrlByMediaId(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        String playUrl = mediaService.getPlayUrlByMediaId(mediaId,companyId);

        return playUrl;
    }
}